package com.myway.ibe.command;

import com.myway.ibe.domain.IBEPassenger;
import com.myway.ibe.domain.IBEPnr;
import com.myway.ibe.domain.IBESegment;
import com.myway.ibe.enums.PassengerType;
import com.myway.ibe.exception.IBEException;
import com.travelsky.ibe.client.pnr.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

/**
 * Created by Administrator on 2018/1/15.
 * 提取PNR
 */
@Slf4j
public class RetrievePNRCommand extends BaseIBECommand {
    @Override
    public void execute(Map<String, Object> params) {
        //获取PNR编号
        String PNR=(String)params.get("PNR");
        RT rt=new RT();
        //连接IBE
        this.configIBEClient(rt);
        //New instance....
        IBEPnr pnr=new IBEPnr();
        try {
            //根据PNR编号提取PNR信息
            RTResult result=rt.retrieve(PNR);
            log.debug("retrieve pnr "+PNR);
            //设置pnr编号
            pnr.setPnrNo(result.getPnrcode());
            //设置officeNo
            pnr.setOfficeNo(result.getResp().getOfficecode());
            log.debug("office code "+pnr.getOfficeNo());
            //获取现行部分所有航段组
            for(PNRAirSeg seg:(Vector<PNRAirSeg>)result.getAirSegs()){
                IBESegment ibeSegment=new IBESegment();
                BeanUtils.copyProperties(seg, ibeSegment);
                ibeSegment.setLineIndex(seg.getIndex());
                Assert.isTrue(ibeSegment.getLineIndex() >= 0);
                ibeSegment.setFlightNo(seg.getAirNo());
                pnr.getSegments().add(ibeSegment);
                log.debug("segment "+ibeSegment.getOrgCity()+ " -" +ibeSegment.getDesCity()+" action code "+ibeSegment.getActionCode());
            }
            //copy pax...
            Map<String,IBEPassenger> paxMap=new HashMap<>();
            /**
             * PNRPassenger
             * 本类包含一个PNR旅客组的信息。旅客有成人、儿童和无陪伴儿童三种类别。 对于不同类别的旅客，
             * 该类中的有效字段个数会有所区别。比如，成人旅客 和儿童旅客没有年龄信息，这时相应的提取函数返回空值或者0值。
             * 注意没有婴儿
             */
            //设置旅客组的信息
            for(PNRPassenger pax:(Vector<PNRPassenger>)result.getPassengers()){
                IBEPassenger ibePax=new IBEPassenger();
                ibePax.setName(pax.getName());
                ibePax.setType(pax.getType()==1? PassengerType.CHILD:PassengerType.ADULT);
                paxMap.put(pax.getName(), ibePax);
                pnr.getPassengers().add(ibePax);
                log.debug("pax "+pax.getName());
            }
            //获取婴儿组
            for(PNRInfant infant:(Vector<PNRInfant>)result.getInfants()){
                String carrier=infant.getCarrier();
                if(paxMap.containsKey(carrier)){
                    IBEPassenger ibePax=paxMap.get(carrier);
                    ibePax.setInfantName(infant.getName());
                    log.debug("infant "+infant.getName()+" Accompany"+ibePax.getName());
                }
            }
        } catch (Exception e) {
            log.error("Retrieve pnr error with pnr no"+PNR);
            log.error(e.getMessage(),e.getCause());
            throw new IBEException(e.getMessage());
        }
        params.put("IBEPnr", pnr);
    }
}
